package com.ts.audiocamera

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import android.util.Log
import androidx.activity.enableEdgeToEdge
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import com.ts.audioplayer.MusicService
import com.ts.audioplayer.TsPlayer
import com.ts.audioplayer.listener.IPlayerListener
import com.ts.audioplayer.listener.MNOnParparedListener
import java.io.File


class AudioActivity : AppCompatActivity() {
    private  val TAG = "AudioActivity"
    var tsPlayer:TsPlayer?=null
    @RequiresApi(Build.VERSION_CODES.O)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_audio)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }


        // 方案一：跳转到系统文件访问页面，手动赋予
//        val intent: Intent =
//            Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION)
//
//        intent.setData(Uri.parse("package:" + this.packageName))
//        startActivity(intent)

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE ) != PackageManager.PERMISSION_GRANTED) {
            // 权限未授予，请求权限
            ActivityCompat.requestPermissions(this,
                arrayOf(
                    Manifest.permission.CAMERA, Manifest . permission . WRITE_EXTERNAL_STORAGE,
                    Manifest . permission . MANAGE_EXTERNAL_STORAGE,
                    Manifest.permission.RECORD_AUDIO,
                    Manifest.permission.READ_EXTERNAL_STORAGE), 12);
        } else {
            // 权限已授予，打开摄像头
//            surfaceView=findViewById(R.id.camera_view) as SurfaceView
//            surfaceView?.holder?.addCallback(this)
        }


        tsPlayer= TsPlayer()
        tsPlayer?.source="/sdcard/xiaowei.mp3"
        tsPlayer?.mnoOnParparedListener=object : MNOnParparedListener {
            override fun onParpared() {
                Log.e(TAG,"AudioActivity")
                tsPlayer?.start()
            }
        }
        tsPlayer?.playerListener=object: IPlayerListener {
            override fun onLoad(load: Boolean) {

            }

            override fun onCurrentTime(currentTime: Int, totalTime: Int) {

            }

            override fun onError(code: Int, msg: String?) {

            }

            override fun onPause(pause: Boolean) {

            }

            override fun onDbValue(db: Int) {

            }

            override fun onComplete() {

            }

            override fun onNext(): String? {
                return ""
            }
        }
        var mFile= File("/sdcard/xiaowei.mp3")
        if (mFile.exists()){
            Log.e(TAG,"david+pppooo")
        }else{
            Log.e(TAG,"david+pppoorerero")
        }
        tsPlayer?.prepare()
//        var mMusicService= MusicService()
//        startForegroundService(Intent(this,MusicService::class.java))
    }
    private fun optMusic(action: String) {
        LocalBroadcastManager.getInstance(this).sendBroadcast(Intent(action))
    }

    override fun onDestroy() {
        super.onDestroy()
        stopService(Intent(this,MusicService::class.java))
    }
}